package com.ming.mallchat.common.user.controller;

import com.ming.mallchat.common.common.domain.vo.resp.ApiResult;
import com.ming.mallchat.common.common.utils.AssertUtil;
import com.ming.mallchat.common.common.utils.RequestHolder;
import com.ming.mallchat.common.user.domain.dto.ItemInfoDTO;
import com.ming.mallchat.common.user.domain.dto.SummeryInfoDTO;
import com.ming.mallchat.common.user.domain.vo.req.user.*;
import com.ming.mallchat.common.user.domain.vo.resp.user.BadgeResp;
import com.ming.mallchat.common.user.service.RoleService;
import com.ming.mallchat.common.user.service.UserService;
import com.ming.mallchat.common.user.domain.vo.resp.user.UserInfoResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 86150
 * @date 2023/09/28
 */
@RestController
@Slf4j
@RequestMapping("/capi/user")
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取用户信息",notes = "嘿嘿")
    @GetMapping("/userInfo")
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(userService.userinfo(RequestHolder.get().getUid()));
    }

    @ApiOperation(value = "用户修改名称",notes = "嘿嘿")
    @GetMapping("/name")
    public ApiResult<Void> modifyName(@Valid @RequestBody ModifyNameReq req) {
        userService.modifyName(req.getName(),RequestHolder.get().getUid());
        return ApiResult.success();
    }

    @ApiOperation(value = "徽章列表",notes = "嘿嘿")
    @GetMapping("/badges")
    public ApiResult<List<BadgeResp>> badges() {
        return ApiResult.success(userService.badges(RequestHolder.get().getUid()));
    }

    @ApiOperation(value = "佩戴徽章",notes = "嘿嘿")
    @PutMapping("/badge")
    public ApiResult<Void> badge(@Valid @RequestBody BadgeReq req) {
        userService.badge(RequestHolder.get().getUid(),req.getItemId());
        return ApiResult.success();
    }

    @ApiOperation(value = "拉黑用户",notes = "嘿嘿")
    @PutMapping("/black")
    public ApiResult<Void> black(@Valid @RequestBody BlackReq req) {
        Long uid = RequestHolder.get().getUid();
        boolean hasPower = roleService.hasRole(uid);
        AssertUtil.isTrue(hasPower,"没有操作权限");
        userService.black(req.getUid());
        return ApiResult.success();
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoReq req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation("徽章聚合信息-返回的代表需要刷新的")
    public ApiResult<List<ItemInfoDTO>> getItemInfo(@Valid @RequestBody ItemInfoReq req) {
        return ApiResult.success(userService.getItemInfo(req));
    }

}
